#include "qlifetable.h"

#include <QHeaderView>
#include <iostream>
#include <stdlib.h>

#include "qlife.h"

QLifeTable::QLifeTable ( QWidget *parent ) : QTableWidget ( 0, 2, parent )
{
	horizontalHeader() -> hide();
	verticalHeader() -> hide();

	horizontalHeader() -> setResizeMode ( QHeaderView::Stretch );

	setShowGrid ( false );

	lastId = -1;
}


QLifeTable::~QLifeTable()
{}

void QLifeTable::receiveLife ( long id, QStringList data )
{
	if ( lastId != id )
	{
		clearContents();
		setRowCount ( 0 );
	}

	int j = 0;

	for ( int i = 0; i < data.size() / 2; ++i )
	{
		QTableWidgetItem* label;
		QTableWidgetItem* value;

		if ( rowCount() <= i )
		{
			insertRow ( i );

			label = new QTableWidgetItem();
			value = new QTableWidgetItem();

			setItem ( i, 0, label );
			setItem ( i, 1, value );
		}
		else
		{
			label = item ( i, 0 );
			value = item ( i, 1 );
		}

		label -> setText ( data.at ( j ) );
		j++;
		value -> setText ( data.at ( j ) );
		j++;
	}

	if ( lastId != id )
	{
		for ( int i = 0; i < rowCount(); ++i ) setRowHeight ( i, 20 );
	}

	lastId = id;
}
